<?php if ($_POST) { 
    include("siteTop.php"); ?>

<!--    
    Created By : Julian Ponce
    B.S. Computer Science - De La Salle Canlubang
    November 15, 2011
    Description :
    
    This page allows the admin to view the account details of a particular userId.
-->
    
<?php 
    // THIS SERVES AS THE TEMPORARY ACCOUNT USER ID WHILE 
    // WAITING FOR POST FROM ACCOUNT LIST TO BE FINISHED.
    $accountUserId = $_POST['accountUserId'];
   
    //CONNECT TO DATABASE
    $connect = mysql_connect($hostName,$rootName,$dBasePassword) or die ('Unable to connect!');
    mysql_select_db($dBaseName) or die ('Unable to select database!');
    
    // IDENTIFIES THE ACCOUNT DETAILS
    $query = 
        "
            SELECT *
            FROM Account
            WHERE userId = '$accountUserId'
        ";
    
    $result = mysql_query($query) or die ('Error in query: $query. ' . mysql_error());
    $row = mysql_fetch_array($result);
    
    $accountUsername = $row['username'];
    $accountPassword = $row['password'];
    $accountLastName = $row['lastName'];
    $accountFirstName = $row['firstName'];
    $accountMiddleName = $row['middleName'];
    $accountStatus = $row['status'];
    
?>
<!-- HEADER START -->
    <!--JAVASCRIPT SOURCES-->
    <script src="js/jquery-1.6.2.min.js"></script>

    <!--ACTUAL JS SCRIPTS AND JQUERIES-->
    <script type="text/javascript">
        jQuery(document).ready(function(){
            accountStatusJS = "<?php echo $accountStatus; ?>";
            
            // [START] GET ALL POSITION/S OF USER
            jQuery.ajax({
                type : 'POST',
                url : 'ajaxRequests/admGetUserPosition.php',
                dataType : 'json',
                data: {
                    accountUserId : "<?php echo $accountUserId; ?>"
                },
                success : function(data){
                  if (data) {
                      
                    // PUT POSITIONS TO TABLE POSITION
                    jQuery.each(data, function(index, data) {
                        jQuery("#positionLastRow").before("<tr><td>"+data.departmentName+"</td><td>"+data.positionName+"</td><td>"+data.status+"</td></tr>");
                    });
                  }                  
                },
                error : function() {
                    alert("fail in AJAX [admGetUserPosition.php]");
                }
            }); // [END] GET ALL POSITION/S OF USER
            
            // [START] AJAX IN CHECKING IF THIS ACCOUNT CAN BE EDITABLE.
            // THIS MEANS THAT THE ACCOUNT HAS ON GOING TRANSACTIONS.
            jQuery.ajax({
                type : 'POST',
                url : 'ajaxRequests/admCheckAccount.php',
                dataType : 'json',
                data: {
                    accountUserId : "<?php echo $accountUserId; ?>"
                },
                success : function(data){
                   
                   // IF DATA/"1" = THE AJAX HAS RETURNED STEP ID'S FOR THIS ACCOUNT.
                   // THIS MEANS THAT THE ACCOUNT HAS ON GOING TRANSACTIONS.
                   // ACCOUNT CANNOT BE MODIFIED/EDITED.
                   if (data) {
                       jQuery("#editButton").hide();
                       jQuery("#cannotEditMsg").show();
                   }
                   
                   // ELSE = ACCOUNT HAS NO ON GOING TRANSACTIONS, IT CAN BE EDITED.
                   else {
                       jQuery("#editButton").show();

                        // CHECK THE STATUS OF THE ACCOUNT AND IDENTIFY WHAT BUTTON TO SHOW
                        // EITHER ACTIVATE BUTTON / DEACTIVATE BUTTON
                        if (accountStatusJS == "Active") {
                            jQuery("#deactivateButton").show();
                            jQuery("#activateButton").hide();
                        }

                        else {                          
                            jQuery("#deactivateButton").hide();
                            jQuery("#activateButton").show();
                        }
                   }
                   
                },
                error : function() {
                    alert("fail in AJAX [admCheckAccount.php]");
                }
            }); // [END] AJAX IN CHECKING IF THIS ACCOUNT CAN BE EDITABLE.
            
            // [START] BACK BUTTON CLICKED > REDIRECT TO admAccountSetup.php
            jQuery("#backButton").click(function() {
                    window.location = 'admAccountSetup.php';                
            });
            
            // [START] EDIT BUTTON CLICKED > submit accountEditForm and redirect user to admAccountEdit.php
            jQuery("#editButton").click(function() {
                tempUserId = <?php echo $accountUserId; ?>;
                
                // SAVE PASSED USER ID
                jQuery("#passedUserId").val(tempUserId);

                // SUBMIT FORM FOR EDIT
                jQuery("#accountEditForm").submit();
            });
            
            // [START] ACTIVATE OR DEACTIVATE BUTTON WAS CLICKED
            jQuery(".actDeactButton").click(function() {
                buttonVal = this.value

                // [START] AJAX TO ACTIVATE-DEACTIVATE ACCOUNT
                jQuery.ajax({
                    type : 'POST',
                    url : 'ajaxRequests/admActDeactAccount.php',
                    dataType : 'json',
                    data: {
                        accountUserId : "<?php echo $accountUserId; ?>",
                        action : buttonVal
                    },
                    success : function(data){
                        
                        // SUCCESS MESSAGE FOR ACTIVATED
                        if (buttonVal == "Activate") {
                            jQuery("#passedMessage").val("You have successfully activated Employee ID : <?php echo $accountUsername;?>.");
                        }
                        
                        // SUCCESS MESSAGE FOR DEACTIVATED
                        else {
                            jQuery("#passedMessage").val("You have successfully deactivated Employee ID : <?php echo $accountUsername;?>.");
                        }
                        
                        // SUBMIT FORM FOR SUCCESS MESSAGE
                        jQuery("#successMessageForm").submit();
                    },
                    error : function() {
                        alert("fail in AJAX [admActDeactAccount.php]");
                    }
                }); // [END] AJAX TO ACTIVATE-DEACTIVATE ACCOUNT
                
            }); // [END] ACTIVATE OR DEACTIVATE BUTTON WAS CLICKED
            
            
        }); // END - (DOCUMENT READY FUNCTION)
    </script>
    <!-- HEAD END -->


<?php include("siteMiddle.php"); ?>
    
<!-- START OF BODY -->
<div id="content">
    <h1>Account View Page</h1>
    <form>
        
        <!-- ACCOUNT INFORMATION -->
        <span class="screenlet" >Account Information</span>
        <table border="1">
            <tr>
                <td>User Id</td>
                <td><?php echo $accountUserId; ?></td>
            </tr>
            <tr>
                <td>Username</td>
                <td><?php echo $accountUsername; ?></td>
            </tr> 
            <tr>
                <td>Password</td>
                <td><?php echo $accountPassword; ?></td>
            </tr>
            <tr>
                <td>Last Name</td>
                <td><?php echo $accountLastName; ?></td>
            </tr>
            <tr>
                <td>First Name</td>
                <td><?php echo $accountFirstName; ?></td>
            </tr>
            <tr>
                <td>Middle Name</td>
                <td><?php echo $accountMiddleName; ?></td>
            </tr>
            <tr>
                <td>Status</td>
                <td><?php echo $accountStatus; ?></td>
            </tr>
        </table>
        
        <!-- ACCOUNT POSITIONS -->
        <span class="screenlet" >Account Position/s</span>
        <table border="1">
            <tr>
                <th>Department</th>
                <th>Position</th>
                <th>Status</th>
            </tr>
            <tr id="positionLastRow"/>
        </table>
     
        <!-- MESSAGE IF ACCOUNT CANNOT BE EDITED -->
        <span id="cannotEditMsg" style="display:none;">This account cannot be modified<br>because it has on-going transactions.<br><br></span>
        
    <!-- EDIT BUTTON -->
    <input type="Button" name="editButton" id="editButton" class="siteGreenButton" value="Edit" style="display:none;"/>
    
    <!-- ACTIVATE BUTTON -->
    <input type="Button" name="activateButton" id="activateButton" class="siteGreenButton actDeactButton" value="Activate" style="display:none;"/>
    
    <!-- DEACTIVATE BUTTON -->
    <input type="Button" name="deactivateButton" id="deactivateButton" class="siteRedButton actDeactButton" value="Deactivate" style="display:none;"/>
  
    <!-- BACK BUTTON -->
    <input type="Button" name="backButton" id="backButton" class="siteGreenButton" value="Back"/>
    </form>
    
    <!-- THIS WILL SERVE AS THE FORM IN ORDER TO PASS userId TO admAccountEdit.php -->
    <form id="accountEditForm" action="admAccountEdit.php" method="POST" target="_self">
        <input type="hidden" name="userId" id="passedUserId"value=""/>
    </form>
    
    <form id="successMessageForm" action="admAccountSetup.php" method="POST" target="_self">
        <input type="hidden" name="accSuccessMessage" id="passedMessage"value=""/>
    </form>

</div> 

<!-- BODY END -->
<?php 
    include ("siteBottom.php"); 
    }
    
    else {
        echo "You are not allowed to view this page. This incident will be reported immediately.";
    }
?>